package com.cloudbroker.bcs.platform.ltb.middle.util;

import com.alibaba.fastjson.JSON;
import com.cloudbroker.bcs.platform.ltb.middle.entity.BlackWord;
import org.apache.commons.collections.map.HashedMap;

import java.util.*;

/**
 * Created by lili19289 on 2016/7/20.
 */
public class StringReplacer {


    public static StringReplacer instance;

    private static Map<String,WordReplace> replacerMap = new HashedMap();

    public static void init(List<BlackWord> blackWords){
        Map<String,List<String>> wordsMap = new HashedMap();
        for(BlackWord word : blackWords){
            List<String> wordContents =wordsMap.get(word.getCompany_id());
            if(wordContents == null){
                wordContents = new ArrayList<>();
            }
            wordContents.add(word.getBlackword_content());
            wordsMap.put(word.getCompany_id(),wordContents);
        }
        for(Map.Entry entry : wordsMap.entrySet()){
            String company_id = entry.getKey().toString();
            List<String> contents = (List)entry.getValue();
            replacerMap.put(company_id,new WordReplace(contents));
        }
    }

    public static boolean  hasSensitiveWord(String src,String company_id){
        WordReplace wordReplace = replacerMap.get(company_id);
        if(wordReplace == null)return false;
        return wordReplace.containsBlackWord(src);
    }


    private static class  WordReplace{

        Map<Character, List<String>> keyIndex = new HashMap<Character, List<String>>();

        public WordReplace(List<String> sensitiveWords) {
            for (String s : sensitiveWords) {
                char c = s.charAt(0);
                List<String> list = keyIndex.get(c);
                if (list == null) {
                    list = new ArrayList<String>();
                }
                list.add(s);
                keyIndex.put(c, list);
            }
            for (Character c : keyIndex.keySet()) {
                List<String> list = keyIndex.get(c);
                Collections.sort(list);
            }
        }

        public  List<String> getByKey(Character key){
            return keyIndex.get(key);
        }


        private void deleteWord(String word){
            char c = word.charAt(0);
            List<String> list = keyIndex.get(c);
            if (list != null) {
                list.remove(word);
            }

        }

        private  boolean containsBlackWord(String src) {
            char[] chars = src.toCharArray();
            int i = 0;
            while (i < chars.length) {
                Character c = chars[i];
                List<String> keys = keyIndex.get(c);
                if (keys != null) {
                    for (String k : keys) {
                        boolean match = true;
                        int j = 1;
                        for (; j < k.length(); j++) {
                            if (i + j > chars.length - 1) {
                                match = false;
                            } else if (chars[i + j] != k.charAt(j)) {
                                // no match
                                match = false;
                                break;
                            }
                        }
                        if (match) {
                            return true;
                        }
                    }
                }
                i++;
            }
            return false;
        }

        public String returnJSONString(){
            return JSON.toJSONString(keyIndex);
        }

    }









}